<template>
  <div class="box app-container">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never">
          <el-col :span="24">
            <el-button type="primary" icon="el-icon-printer" @click="printImage()">打印图片</el-button>
            <el-button type="primary" icon="el-icon-printer" @click="printTable()">打印表格</el-button>
            <el-button type="primary" icon="el-icon-printer"  @click="print()">自定义打印</el-button>
          </el-col>
          <el-col :span="24">
            <el-image :src="src" style="width: 300px;height: 200px;" ref="image"></el-image>
          </el-col>
          <el-col :span="24">
            <el-table
              ref="table"
              :data="tableData"
              style="width: 100%">
              <el-table-column
                prop="date"
                label="日期"
                width="180">
              </el-table-column>
              <el-table-column
                prop="name"
                label="姓名"
                width="180">
              </el-table-column>
              <el-table-column
                prop="address"
                label="地址">
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="24">
            <div class="content" ref="content"></div>
          </el-col>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'print',
  data() {
    return {
      src: 'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg',
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }],
      content: `<h1>Vue Admin Plus</h1>
<p>vue-admin-beautiful.com</p>`
    }
  },
  methods:{
    printImage(){
      this.$print(this.$refs.image)
    },
    printTable(){
      this.$print(this.$refs.table)
    },
    print(){
      this.$prompt('', '自定义打印', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType:'textarea',
        inputValue:this.content
      }).then(({ value }) => {
        this.$refs.content.innerHTML = value
        this.$print(this.$refs.content)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });
    }
  }
}
</script>

<style scoped lang="scss">
.box{
  .el-row{
   .el-col{
     margin-bottom: 20px;
   }
  }
}
</style>
